// pages/playback/playback.js
import {
    OPEN_DOMAIN
} from '../live/config';
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';

let livePlayerContext;
var init;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        accessToken: '',
        deviceSerial: '',
        channelNo: '',
        videoSrc: '',
        coverDisabled: true,
        coverInterval: false,
        showVideoControls: true,
        autoHideTimer: undefined,
        videoLoadingStatus: 0,
        playVideo: false,
        videoNetWorkError: false,
        objectFit: 'contain',
        openSound: true,
        isHD: false,
        showHDSelect: false,
        fullScreen: false,

        deviceOffline: false,
        deviceOfflineTime: new Date(),
        deviceName: '',
        dialogTitle: '',
        dialogContent: '',
        buttons: [{
            text: '知道了'
        }],
        dialogShow: false,
        dialogNodataTitle: '',
        dialogNodataContent: '',
        dialogNodataShow: false,
        cloudPalayParam: null,
        playCode: 0, // 当前播放错误码，
        switch1Checked: true,
        recType: 2, // 1-云存储，2-本地录像
        type: 2, // 2-本地录像回放，3-云存储录像回放
        switchText: '本地',
        param: {}, // 获取播放片段参数
        date: '', // 当前选择日期
        dateLine: [], // 时间片段
        contentItemShow: false, // 暂无数据
        toDay: '', // 今日日期
        page: 1, // 当前页面层级
        durationSeconds:0,
    },
    onLoad: function (options) {
        Toast.loading({
            message: '加载中...',
            forbidClick: true,
            duration: 0,
            // mask:true
        });
        livePlayerContext = wx.createLivePlayerContext('livePlayer');
        console.log('获取参数', options);
        // 获取当前页面栈
        const currentPage = getCurrentPages();
        console.log(currentPage.nv_length);
        // 获取url携带的参数
        const {
            accessToken,
            deviceSerial,
            channelNo,
            date,
            switch1Checked
        } = options
        this.setData({
            playVideo: true,
            accessToken: accessToken,
            deviceSerial: deviceSerial,
            channelNo: channelNo,
            page: currentPage.nv_length,
        });
        console.log(date);
        // 通过分享今日指定日期
        if (date) {
            this.setData({
                date: date,
                toDay: date
            })
        } else {
            // 获取当前日期
            this.getTodayDate();
        }
        if (switch1Checked == "false") {
            this.setData({
                switchText: '云存储',
                switch1Checked: false,
                recType: 1,
                type: 3
            });
        }
        // 获取包含播放片段的时间片段
        this.getTimeLine();
    },
    // 模态框按钮
    tapDialogButton(e) {
        this.setData({
            dialogShow: false
        });
        wx.navigateBack({
            delta: 1
        })
    },
    // 无时间片段数据时
    tapDialogNodataButton() {
        this.setData({
            dialogNodataShow: false
        })
    },
    // 获取存储类型
    switch1Change: function (e) {
        console.log('当前存储类型', e.detail);
        if (e.detail.value == false) {
            this.setData({
                switchText: '云存储',
                switch1Checked: false,
                recType: 1,
                type: 3
            });
            this.getTimeLine();
        } else {
            this.setData({
                switchText: '本地',
                switch1Checked: true,
                recType: 2,
                type: 2
            });
            this.getTimeLine();
        }
    },
    // 获取当天日期
    getTodayDate: function () {
        const that = this;
        let type = null;
        var time = new Date();
        var h = time.getFullYear();
        var m = time.getMonth() + 1;
        var d = time.getDate();
        let date = (h > 9 ? h : '0' + h) + '-' + (m > 9 ? m : '0' + m) + '-' + (d > 9 ? d : '0' + d);
        // let date =  (m > 9 ? m : '0' + m) + '-' + (d > 9 ? d : '0' + d);
        this.setData({
            date: date,
        });
        // 获取当前操作系统类型
        wx.getSystemInfo({
            success(res) {
                console.log('当前操作系统：', res.system);
                const sys = res.system;
                type = sys.split(' ');
                console.log(type);
                if (type[0] == 'iOS') {
                    that.setData({
                        toDay: date.replace(/\-/g, '/')
                    });
                } else {
                    that.setData({
                        toDay: date
                    })
                }
            }
        });
    },


    format: function (now) {
        var time = new Date(now);
        var h = time.getHours(); //返回日期中的小时数（0到23）
        var m = time.getMinutes(); //返回日期中的分钟数（0到59）
        var s = time.getSeconds(); //返回日期中的秒数（0到59）
        return (h > 9 ? h : '0' + h) + ':' + (m > 9 ? m : '0' + m) + ':' + (s > 9 ? s : '0' + s);
    },
    // 计算时间差
    calculateDuration(startTime, endTime) {
        // 将时间字符串分割为时、分、秒  
        const [startH, startM, startS] = startTime.split(':').map(Number);
        const [endH, endM, endS] = endTime.split(':').map(Number);
        // 计算秒数差  
        const totalSecondsStart = startH * 3600 + startM * 60 + startS;
        const totalSecondsEnd = endH * 3600 + endM * 60 + endS;
        // 计算差值  
        const diffInSeconds = totalSecondsEnd - totalSecondsStart;
        // 将秒数差转换为小时、分钟和秒  
        let hours = Math.floor(diffInSeconds / 3600);
        let minutes = Math.floor((diffInSeconds % 3600) / 60);
        let seconds = diffInSeconds % 60;
        // 确保每个单位都是两位数  
        hours = hours.toString().padStart(2, '0');
        // minutes = minutes.toString().padStart(2, '0');  
        seconds = seconds.toString().padStart(2, '0');
        // 返回格式化的时间差  
        return `${minutes}'${seconds}'`;
    },
    // 将时间字符串转换为秒数  
    timeStrToSeconds(timeStr) {
        let [hours, minutes, seconds] = timeStr.split(':').map(Number);
        return hours * 3600 + minutes * 60 + seconds;
    },
    // 获取时间轴
    getTimeLine: function () {
        const {
            recType,
            date,
            accessToken,
            deviceSerial,
            channelNo,
            timelineTimer,
            currentTimer
        } = this.data;

        console.log('获取到的accessToken, accessToken', accessToken);
        var _this = this;
        let currentDate = null;
        let timestampCurrent = null;
        let timestampToday = null;
        // 时间戳转换：Date.parse(new Date("2020-09-23 12:13:56"))
        if (date) {
            currentDate = date;
            const time = currentDate + ' ' + '00:00:00';
            const endTime = currentDate + ' ' + '23:59:59'
            timestampToday = Date.parse(new Date(time.replace(/\-/g, '/'))); // 选择日期凌晨时间戳
            timestampCurrent = Date.parse(new Date(endTime.replace(/\-/g, '/'))); // 选择日期时间戳
        }
        wx.request({
            url: `${OPEN_DOMAIN}/api/lapp/video/by/time`,
            method: 'POST',
            data: {
                accessToken: accessToken,
                deviceSerial: deviceSerial,
                channelNo: channelNo,
                recType: recType,
                startTime: timestampToday,
                endTime: timestampCurrent
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            success: (res) => {
                console.log('获取播放时间片段', res.data);
                if (res.data.code == 200) {
                    if (res.data.data != null) {
                        var result = res.data.data;
                        const len = result.length;
                        let availArr = [];
                        for (let i = len - 1; i >= 0; i--) {
                            let res = result[i];
                            let et = null;
                            let st = null;
                            let duration = null;//分钟+秒
                            let durationSeconds = null;//秒
                            let timeData=null;//时间段
                            // 最近片段可能存在endTime超过当前时间
                            if (i == (len - 1) && (res.endTime > timestampCurrent)) {
                                et = this.format(timestampCurrent)
                            } else {
                                et = this.format(res.endTime)
                            }
                            // 存在startTime可能为前一天的时间
                            if (i == 0 && (res.startTime < timestampToday)) {
                                st = this.format(timestampToday)
                            } else {
                                st = this.format(res.startTime);
                            }
                            duration = this.calculateDuration(st, et);
                            durationSeconds =this.timeStrToSeconds(et)  - this.timeStrToSeconds(st);
                            timeData=st.slice(0, 2);
                            availArr.push({
                                st: st,
                                et: et,
                                duration: duration,
                                durationSeconds:durationSeconds,
                                timeData:timeData,
                                id:i
                            })
                        }
                        console.log('availArr:', availArr);
                        let timeQuantum00=false;
                        let timeQuantum01=false;
                        let timeQuantum02=false;
                        let timeQuantum03=false;
                        let timeQuantum04=false;
                        let timeQuantum05=false;
                        let timeQuantum06=false;
                        let timeQuantum07=false;
                        let timeQuantum08=false;
                        let timeQuantum09=false;
                        let timeQuantum10=false;
                        let timeQuantum11=false;
                        let timeQuantum12=false;
                        let timeQuantum13=false;
                        let timeQuantum14=false;
                        let timeQuantum15=false;
                        let timeQuantum16=false;
                        let timeQuantum17=false;
                        let timeQuantum18=false;
                        let timeQuantum19=false;
                        let timeQuantum20=false;
                        let timeQuantum21=false;
                        let timeQuantum22=false;
                        let timeQuantum23=false;
                        availArr.forEach(item=>{
                            if(item.timeData=='00'){
                                timeQuantum00=true
                            }else   if(item.timeData=='01'){
                                timeQuantum01=true
                            }else   if(item.timeData=='02'){
                                timeQuantum02=true
                            }else   if(item.timeData=='03'){
                                timeQuantum03=true
                            }else   if(item.timeData=='04'){
                                timeQuantum04=true
                            }else   if(item.timeData=='05'){
                                timeQuantum05=true
                            }else   if(item.timeData=='06'){
                                timeQuantum06=true
                            }else   if(item.timeData=='07'){
                                timeQuantum07=true
                            }else   if(item.timeData=='08'){
                                timeQuantum08=true
                            }else   if(item.timeData=='09'){
                                timeQuantum09=true
                            }else   if(item.timeData=='10'){
                                timeQuantum10=true
                            }else   if(item.timeData=='11'){
                                timeQuantum11=true
                            }else   if(item.timeData=='12'){
                                timeQuantum12=true
                            }else   if(item.timeData=='13'){
                                timeQuantum13=true
                            }else   if(item.timeData=='14'){
                                timeQuantum14=true
                            }else   if(item.timeData=='15'){
                                timeQuantum15=true
                            }else   if(item.timeData=='16'){
                                timeQuantum16=true
                            }else   if(item.timeData=='17'){
                                timeQuantum17=true
                            }else   if(item.timeData=='18'){
                                timeQuantum18=true
                            }else   if(item.timeData=='19'){
                                timeQuantum19=true
                            }else   if(item.timeData=='20'){
                                timeQuantum20=true
                            }else   if(item.timeData=='21'){
                                timeQuantum21=true
                            }else   if(item.timeData=='22'){
                                timeQuantum22=true
                            }else   if(item.timeData=='23'){
                                timeQuantum23=true
                            }
                        })
                        this.setData({
                            dateLine: availArr,
                            timeQuantum00:timeQuantum00,
                            timeQuantum01:timeQuantum01,
                            timeQuantum02:timeQuantum02,
                            timeQuantum03:timeQuantum03,
                            timeQuantum04:timeQuantum04,
                            timeQuantum05:timeQuantum05,
                            timeQuantum06:timeQuantum06,
                            timeQuantum07:timeQuantum07,
                            timeQuantum08:timeQuantum08,
                            timeQuantum09:timeQuantum09,
                            timeQuantum10:timeQuantum10,
                            timeQuantum12:timeQuantum12,
                            timeQuantum13:timeQuantum13,
                            timeQuantum14:timeQuantum14,
                            timeQuantum15:timeQuantum15,
                            timeQuantum16:timeQuantum16,
                            timeQuantum17:timeQuantum17,
                            timeQuantum18:timeQuantum18,
                            timeQuantum19:timeQuantum19,
                            timeQuantum20:timeQuantum20,
                            timeQuantum21:timeQuantum21,
                            timeQuantum22:timeQuantum22,
                            timeQuantum23:timeQuantum23,
                        })
                    } else {
                        this.setData({
                            //   dialogNodataTitle: '暂无数据',
                            //   dialogNodataContent: ' 暂无录像片段',
                            //   dialogNodataShow: true,
                            dateLine: [],
                        });
                        Dialog.alert({
                            message: '暂无录像片段',
                        }).then(() => {});
                        this.handleStop();
                    }
                } else if (res.data.code == '10002') {
                    this.setData({
                        // dialogTitle: '',
                        // dialogContent: 'accessToken过期或异常',
                        // dialogShow: true,
                        dateLine: [],
                    })
                    Dialog.alert({
                        message: 'accessToken过期或异常',
                    }).then(() => {
                        wx.navigateBack()
                    });

                } else if (res.data.code == "20002") {
                    this.setData({
                        dateLine: [],
                    })
                    Diaog.alert({
                        mlessage: '设备不存在',
                    }).then(() => {
                        wx.navigateBack()
                    });
                } else if (res.data.code == "20007") {
                    this.setData({
                        dateLine: [],
                    })
                    Dialog.alert({
                        message: '设备不在线',
                    }).then(() => {
                        wx.navigateBack()
                    });
                } else if (res.data.code == "20008") {
                    this.setData({
                        dateLine: [],
                    });
                    Dialog.alert({
                        message: '响应超时',
                    }).then(() => {
                        wx.navigateBack()
                    });
                } else {
                    this.setData({
                        dateLine: [],
                    })
                    Dialog.alert({
                        message: res.data.msg,
                    }).then(() => {
                        wx.navigateBack()
                    });
                }
                Toast.clear();
            }
        })
    },
  
    // 点击录像片段
    onPlay(e) {
        console.log(e);
        console.log(e.currentTarget.dataset.item.st);
        this.setData({
            durationSeconds:e.currentTarget.dataset.item.durationSeconds,
            idinfo:e.currentTarget.dataset.item.id,
        })
        const {
            type,
            date,
            accessToken,
            deviceSerial,
            channelNo,
            videoSrc,
        } = this.data;

        const param = {
            'accessToken': accessToken,
            'channelNo': channelNo,
            'deviceSerial': deviceSerial,
            'protocol': 3, // 流播放协议，1-ezopen、2-hls、3-rtmp
            'startTime': '2024-09-11 ' + e.currentTarget.dataset.item.st,
            'stopTime': '2024-09-11 ' + e.currentTarget.dataset.item.et,
            'type': type, // 由页面按钮传入值
            'expireTime': 86400,
            "supportH265": "1",
            "quality": "2"
        }
        // 获取播放地址
        this.getPlayUrl(param);
    },

    // 获取播放地址
    getPlayUrl(param) {
        var _this = this;
        wx.request({
            url: `${OPEN_DOMAIN}/api/lapp/v2/live/address/get`,
            method: 'POST',
            data: param,
            header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            success: (res) => {
                console.log('获取到的播放地址：', res.data);
                if (res.data.code == "200" && res.data.data && res.data.data.url) {
                    const playUrl = res.data.data.url;
                    this.setData({
                        videoSrc: playUrl
                    });
                    // 先停止
                    this.handleStop();
                    // 开始播放
                    setTimeout(() => {
                        this.handlePlay();
                    }, 1000)
                    console.log('当前播放地址：', this.data.videoSrc);
                } else if (res.data.code == "20007") {
                    //   debugger;
                    Dialog.alert({
                        message: '设备不在线',
                    }).then(() => {});
                } else if (res.data.code == "20002") {
                    Dialog.alert({
                        message: '设备不存在',
                    }).then(() => {});
                } else if (res.data.code == "20001") {
                    Dialog.alert({
                        message: '摄像头不存在',
                    }).then(() => {});
                } else if (res.data.code == "20018") {
                    Dialog.alert({
                        message: '用户不拥有该设备',
                    }).then(() => {});
                } else if (res.data.code == "10001") {
                    Dialog.alert({
                        message: '参数错误',
                    }).then(() => {});
                } else if (res.data.code == "60019") {
                    Dialog.alert({
                        message: '设备已被加密，无法继续查看，请前往萤石云app解密。',
                    }).then(() => {});
                } else {
                    //   this.setData({
                    //     dialogTitle: '获取播放地址失败',
                    //     dialogContent: res.data.msg,
                    //     dialogShow: true,
                    //   })
                    Dialog.alert({
                        message: res.data.msg,
                    }).then(() => {});
                    console.log("获取rtmp播放地址失败")
                }
            }
        })
    },

    checkNetWork() {
        const _this = this;
        wx.getNetworkType({
            success(res) {
                const networkType = res.networkType
                if (!networkType || networkType === 'none') {
                    wx.showToast({
                        title: '当前网络异常',
                        icon: 'none',
                        duration: 2000,
                    })
                }
            }
        })
    },

    fullScreen() {
        var _this = this;
        livePlayerContext.requestFullScreen({
            direction: 90,
            success: function () {
                _this.setData({
                    fullScreen: true,
                })
            }
        });
        console.log("开启全屏");
    },
    unfullScreen() {
        var _this = this;
        livePlayerContext.exitFullScreen({
            success: function () {
                _this.setData({
                    fullScreen: false,
                })
            }
        });
        console.log("开启全屏");
    },
    ToggleObjectFit() {
        var objectFit = this.data.objectFit;
        this.setData({
            objectFit: objectFit === 'contain' ? 'fillCrop' : 'contain',
        })
    },
    fullscreenChange(event) {
        console.log("监听到全屏变化", event)
    },
    // 开始播放
    handlePlay(callback) {
        console.log("handelPlay", this.data.playVideo, this.data.isHD);
        this.checkNetWork()
        this.setData({
            videoLoadingStatus: 10,
            videoNetWorkError: false,
        });
        livePlayerContext.play({
            success: () => {
                console.log('开始播放');
                this.setData({
                    // playVideo: true,
                    showVideoControls: true,
                    // videoLoadingStatus: 100,
                    videoNetWorkError: false,
                });
                if (callback && typeof callback === "function") {
                    callback();
                }
            },
            fail: (error) => {
                this.checkNetWork();
                wx.showToast({
                    title: '网络异常',
                    icon: 'none',
                })
                console.log("开始播放失败");
                this.setData({
                    videoNetWorkError: true,
                    showVideoControls: false,
                    videoLoadingStatus: 100,
                })
            }
        })
    },
    handleStop(callback) {
        console.log("stop");
        const {
            list
        } = this.data;
        livePlayerContext.stop({
            success: () => {
                this.setData({
                    playVideo: false,
                    videoLoadingStatus: 0,
                    list: list,
                    panelStatus: 0,
                })
                if (callback && typeof callback === "function") {
                    callback();
                }
            },
            fail: (error) => {
                console.log("停止播放失败")
            }
        })
    },
    autoHideControl() {
        console.log("showHdSelect", this.data.showHDSelect);
        const _this = this;
        clearTimeout(this.data.autoHideTimer);
        this.data.autoHideTimer = setTimeout(() => {
            const {
                showHDSelect
            } = _this.data;
            if (!showHDSelect) {
                this.setData({
                    showVideoControls: false,
                })
            }
        }, 5000);
    },
    handleSound(e) {
        var openSound = this.data.openSound;
        this.setData({
            openSound: !openSound,
        })
    },
    handleHD(e) {
        var showHDSelect = this.data.showHDSelect;
        console.log("handleHD", showHDSelect)
        this.setData({
            showHDSelect: !showHDSelect,
        });
    },
    changeVideoHD(e) {
        var _this = this;
        this.setData({
            showHDSelect: false,
            isHD: true
        });
        this.handleStop(_this.handlePlay);
    },
    changeVideoNormal(e) {
        this.setData({
            showHDSelect: false,
            isHD: false
        })
        this.handleStop(this.handlePlay);
    },
    statechange(e) {
        console.log('live-player code:', e.detail.code, e.detail);
        const {
            code
        } = e.detail;
        let {
            videoLoadingStatus,
            list,
            panelStatus
        } = this.data;
        switch (code) {
            case 2007: //启动loading
                videoLoadingStatus = 0;
                this.setData({
                    playVideo: true,
                    videoLoadingStatus: 0,
                })
                break;
            case 2001: //连接服务器
                videoLoadingStatus = 20 + Math.floor(Math.random() * 10 + 1);
                break;
            case 2002: //已经连接 RTMP 服务器,开始拉流
                videoLoadingStatus = 40 + Math.floor(Math.random() * 10 + 1);
                break;
            case 2008: // 解码器启动
                break;
            case 2009: //视频分辨率改动
                break;
            case 2004: // 视频播放开始
                videoLoadingStatus = 80 + Math.floor(Math.random() * 10 + 1);
                break;
            case 2003: //网络接收到首个视频数据包(IDR)
                videoLoadingStatus = 100;
                this.setData({
                    playVideo: true,
                })
                this.autoHideControl();
                break;
            case 2103: //网络断连, 已启动自动重连（本小程序不自动重连）
                break;
            case 2033: //首次加载出来
                setTimeout(()=>{
                    this.handleStop();//停止
                    console.log('停止播放');
                },this.data.durationSeconds+'000')
                break;
            case 3001:
            case 3002:
            case 3003:
            case 3005: // 播放失败
                videoLoadingStatus = 100;
                this.checkNetWork();
                this.handleStop(this.playError);
                this.setData({
                    showVideoControls: false,
                    videoNetWorkError: true,
                    videoLoadingStatus: 100,
                })
                break;
            case -2301: // 经多次重连抢救无效，更多重试请自行重启播放
                videoLoadingStatus = 100;
                this.setData({
                    showVideoControls: false,
                    videoNetWorkError: true,
                    videoLoadingStatus: 100,
                })
                break;
        }
        this.setData({
            videoLoadingStatus: videoLoadingStatus,
            playCode: code
        })
    },
    getHoursInRange(startTime, endTime) {  
        let startHour = parseInt(startTime.split(':')[0]);  
        let endHour = parseInt(endTime.split(':')[0]);  
        let hours = [];  
  
        for (let hour = startHour; hour <= endHour; hour++) {  
            hours.push(hour.toString().padStart(2, '0')); // 确保小时数是两位数  
        }  
  
        return hours;  
    },  
    playError() {
        this.setData({
            showVideoControls: false,
            videoNetWorkError: true,
            videoLoadingStatus: 100,
        });
    },
    error(e) {
        console.log('live-player', e);
        console.error('live-player error:', e.detail.errMsg)
        if (e.detail.errCode == 10001) {
            wx.showToast({
                title: '视频直播对讲需要你手机授权微信录音或麦克风权限',
                icon: 'none',
                duration: 3000,
            })
        }
    },
    onVideoTap(e) {
        console.log("点击视频");
        const {
            deviceOffline,
            showVideoControls,
            panelStatus,
            videoNetWorkError
        } = this.data;
        if (deviceOffline || panelStatus === 4 || videoNetWorkError) {
            return false;
        }
        if (showVideoControls) {
            this.setData({
                showVideoControls: false,
            });
            clearTimeout(this.data.autoHideTimer);
        } else {
            this.setData({
                showVideoControls: true,
            })
            this.autoHideControl();
        }
    },

    screenShoot(e) {
        const {
            playVideo,
            videoLoadingStatus
        } = this.data;
        if (!playVideo || videoLoadingStatus != 100) {
            console.log("非播放状态下点击截图");
            return false;
        }
        console.log("开始截图")
        // livePlayerContext.snapshot('raw');
        let that = this
        wx.getSetting({
            success(res) {
                if (res.authSetting['scope.writePhotosAlbum']) {
                    that.saveImg();
                } else if (res.authSetting['scope.writePhotosAlbum'] === undefined) {
                    wx.authorize({
                        scope: 'scope.writePhotosAlbum',
                        success() {
                            that.saveImg();
                        },
                        fail() {
                            that.authConfirm()
                        }
                    })
                } else {
                    that.authConfirm()
                }
            }
        })

    },
    saveImg() {
        livePlayerContext.snapshot('raw')
            .then(data => {
                console.log("data", data);
                if (data) {
                    console.log(data)
                    wx.saveImageToPhotosAlbum({
                        filePath: data.tempImagePath,
                        success(res) {
                            console.log("保存成功", res)
                            wx.showToast({
                                title: '截图已保存至手机相册',
                                icon: 'none',
                            })
                        }
                    })
                }
            })
            .catch(err => {
                console.log("err", err);
            })
    },
    // 授权拒绝后，再次授权提示弹窗
    authConfirm() {
        let that = this
        wx.showModal({
            content: '您没打开保存图片权限，是否去设置打开？',
            confirmText: "确认",
            cancelText: "取消",
            success: function (res) {
                if (res.confirm) {
                    wx.openSetting({
                        success: (res) => {
                            if (res.authSetting['scope.writePhotosAlbum']) {
                                that.saveImg();
                            } else {
                                wx.showToast({
                                    title: '您没有授权，无法保存到相册',
                                    icon: 'none'
                                })
                            }
                        }
                    })
                } else {
                    wx.showToast({
                        title: '您没有授权，无法保存到相册',
                        icon: 'none'
                    })
                }
            }
        });
    },

    tapDialogButton(e) {
        this.setData({
            dialogShow: false
        });
        this.pageBack();
    },
    pageBack() {
        wx.navigateBack({
            delta: 1
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        const {
            accessToken,
            deviceSerial,
            channelNo,
            date,
            switch1Checked
        } = this.data;
        console.log(switch1Checked);
        if (res.from === 'button') {
            // 来自页面内转发按钮
            console.log(res.target);
            this.setData({
                panelStatus: 0,
            })
        }
        return {
            title: '小程序',
            path: '/pages/playback/playback?accessToken=' + accessToken + '&deviceSerial=' + deviceSerial + '&channelNo=' + channelNo + '&date=' + date + '&switch1Checked=' + switch1Checked,
        }
    },

    // 时间选择器
    bindDateChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value
        });
        this.getTimeLine();
    },
});